import {Component, Input, OnInit} from '@angular/core';
import {Dbd} from '../../../../model/Dbd';
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";

@Component({
  selector: 'app-outboundedit',
  templateUrl: './outboundedit.component.html',
  styleUrls: ['./outboundedit.component.css']
})
export class OutboundeditComponent implements OnInit {
  @Input() index: string;
  o: Dbd
  isCoffee:boolean
  total=0
  constructor(private adminService: AdminService,
              private alertService: AlertService) {
    this.o = new Dbd()

  }

  ngOnInit(): void {
    this.getOutbound();

  }


  getOutbound() {
    this.adminService.getDbdByid(this.index).then((data:any)=>{
      if(data.msg == "ok"){
        this.o = data.obj
        if(this.o.type=="咖啡"){
          this.adminService.getCoffeeNumByPm(this.o.pm).then((data1:any)=>{
            if(data1.msg == "ok"){
              this.total = data1.obj.total
            }else{
              this.alertService.showError("获取咖啡数量失败")
            }
          })

          this.isCoffee=true
        }else{
          this.isCoffee=false
          this.adminService.getMateriainventoryByPm(this.o.pm).then((data1:any)=>{
            if(data1.msg == "ok"){
              this.total = data1.obj.total
            }else{
              this.alertService.showError("获取包材数量失败")
            }
          })
        }
      }else{
        this.alertService.showError("根据id获取调拨单失败")
      }
    })
  }

  save() {
    this.adminService.saveDbd(this.o).then((data:any)=>{
      if(data.msg == "ok"){
        this.alertService.showSuccess("设置调拨单成功")
      }else{
        this.alertService.showError("设置调拨单失败")
      }
    })
  }


}
